home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
bipl.zip
/
PROCS.ZIP
/
NXTPRIME.ICN
< prev
next >
Wrap
Text File
|
1992-09-28
|
1KB
|
37 lines
############################################################################
#
# File: nxtprime.icn
#
# Subject: Procedure to find the next prime
#
# Author: Gregg Townsend
#
# Date: November 10, 1991
#
###########################################################################
#
# nxtprime(n) returns the next prime number beyond n.
#
############################################################################
procedure nxtprime(n)
local d
static step, div
initial {
step := [1,6,5,4,3,2,1,4,3,2,1,2,1,4,3,2,1,2,1,4,3,2,1,6,5,4,3,2,1,2]
div := [7] # list of known primes
}
n := integer (n) | runerr (101, n)
if n < 7 then # handle small primes specially
return n < (2 | 3 | 5 | 7)
repeat {
n +:= step [n % 30 + 1] # step past multiples of 2, 3, 5
every (d := !div) | |put(div,d:=nxtprime(d)) do { # get test divisors
if n % d = 0 then # if composite, try a larger candidate
break
if d * d > n then # if not divisible up to sqrt, is prime
return n
}
}
end